package com.aabte.springbootswagger.swagger2;

import io.swagger.annotations.*;
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Daniel
 */
@Api(value = "测试接口", tags = {"测试"})
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {

    @ApiOperation(value = "分页获取用户列表")
    @PostMapping("/user/{category}")
    public Result<List<UserDTO>> user(
            @ApiParam(value = "用户分类", required = true)
            @PathVariable("category") String category,
            @ApiParam(value = "分页参数", required = true)
            @RequestBody Page page) {
        log.info("Start user. category: {}, page: {}", category, page);

        List<UserDTO> list = new ArrayList<>();
        list.add(UserDTO.builder().id(1).name("name1").build());
        list.add(UserDTO.builder().id(2).name("name2").build());
        list.add(UserDTO.builder().id(3).name("name3").build());
        list.add(UserDTO.builder().id(4).name("name4").build());

        Result<List<UserDTO>> result = Result.<List<UserDTO>>builder()
                .innerCode("0")
                .message("succeed")
                .data(list)
                .build();

        log.info("End user. result: {}", result);
        return result;
    }


    @Data
    @ApiModel
    public static class Page {
        @ApiModelProperty("当前页码")
        private Integer pageNo;
        @ApiModelProperty("每页数量")
        private Integer pageSize;
    }

    @Data
    @Builder
    @ApiModel(value = "通用返回定义类")
    public static class Result<T> {
        @ApiModelProperty(value = "状态码")
        private String innerCode;
        @ApiModelProperty(value = "状态码对应的描述信息")
        private String message;
        @ApiModelProperty(value = "响应数据")
        private T data;
    }

    @Data
    @Builder
    @ApiModel(value = "测试数据传输类")
    public static class UserDTO {
        @ApiModelProperty(value = "用户ID")
        private Integer id;
        @ApiModelProperty(value = "用户名")
        private String name;
    }

}
